package ArrayList;

import java.util.ArrayList;

public class User_test {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        // 创建用户对象
        User u1 = new User("001", "zhangsan", "123456");
        User u2 = new User("002", "lisi", "12345678");

        list.add(u1);
        list.add(u2);
        // 判断id是够存在
        System.out.println(contains(list, "001"));
        // 返回id索引
        System.out.println(getIndex(list, "001"));


    }

    public static boolean contains(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public static int getIndex(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

}
